package com.leyou.user.controller;


import com.leyou.common.auth.entity.UserHolder;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.user.dto.UserAddressDTO;
import com.leyou.user.dto.UserDTO;
import com.leyou.user.entity.TbUser;
import com.leyou.user.entity.TbUserAddress;
import com.leyou.user.service.TbUserAddressService;
import com.leyou.user.service.TbUserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class UserAddressController {
//    GET /address/byId?id=12

    @Autowired
    private TbUserAddressService userAddressService;

    @GetMapping("/address/byId")
    public ResponseEntity<UserAddressDTO> findUserAddressById(@RequestParam("id") Long id){
        TbUserAddress tbUserAddress = userAddressService.getById(id);
        UserAddressDTO userAddressDTO = BeanHelper.copyProperties(tbUserAddress, UserAddressDTO.class);
        return ResponseEntity.ok(userAddressDTO);
    }
//   /address/list
    @GetMapping("/address/list")
    public ResponseEntity<List<UserAddressDTO>> findUserAddressByUserId(){
        String userId = UserHolder.getUserId();
        List<UserAddressDTO> userAddressDTOList = userAddressService.findUserAddressByUserId(Long.parseLong(userId));
        return ResponseEntity.ok(userAddressDTOList);
    }

}
